/*
 *
 *      Copyright (c) 2018-2025, st All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the pig4cloud.com developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: st
 *
 */

package com.mkh.st.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mkh.st.admin.api.dto.*;
import com.mkh.st.admin.api.vo.*;
import com.mkh.st.admin.service.SysRoleService;
import com.mkh.st.common.core.util.R;
import com.mkh.st.common.log.annotation.SysLog;
import com.mkh.st.common.security.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author st
 * @date 2020-02-10
 */
@RestController
@AllArgsConstructor
@RequestMapping("/role")
@Api(value = "role", tags = "角色管理模块")
public class SysRoleController {

	private final SysRoleService sysRoleService;

	/**
	 * 角色列表分页查询
	 * @param dto
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/getRoleInfo")
	public R<IPage<RoleInfoVO>> getRoleInfo(Page page,RolePageDTO dto) {
		dto.setTenantId(SecurityUtils.getUser().getTenantId());
		return sysRoleService.getRoleInfo(page,dto);
	}

	@ApiOperation(value = "选择角色查询", notes = "选择角色查询")
	@GetMapping("/getRoleInfoList")
	public R<List<RoleInfoVO>> getRoleInfoList(RolePageDTO dto) {
		dto.setTenantId(SecurityUtils.getUser().getTenantId());
		return R.ok(sysRoleService.getRoleInfoList(dto)) ;
	}

	/**
	 * 角色树查询
	 * @param dto
	 * @return
	 */
	@ApiOperation(value = "角色树查询", notes = "角色树查询")
	@GetMapping("/getRoleInfoTree")
	public R<List<RoleTree>> getRoleInfoTree(RolePageDTO dto) {
		return R.ok(sysRoleService.getRoleInfoTree(dto));
	}

	@ApiOperation(value = "角色详情", notes = "角色详情")
	@GetMapping("/getSysRoleDetail")
	public R<RoleDetailVO> getSysRoleDetail(@RequestParam Long roleId) {
		return R.ok(sysRoleService.getSysRoleDetail(roleId));
	}

	@ApiOperation(value = "获取当前租户全部角色", notes = "获取当前租户全部角色")
	@GetMapping("/getSysRoleList")
	public R<List<RoleListVO>> getSysRoleList() {
		return sysRoleService.getSysRoleList();
	}

	@ApiOperation(value = "获取当前角色的上级层级名称", notes = "获取当前角色的上级层级名称")
	@GetMapping("/getHierarchyName")
	public R<String> getHierarchyName(@RequestParam Long roleId) {
		return R.ok(sysRoleService.getHierarchyName(roleId));
	}

	@SysLog("保存角色")
	@ApiOperation(value = "保存角色", notes = "保存角色")
	@PostMapping("/saveRole")
	public R<Boolean> saveRole(@Valid @RequestBody SaveRoleDTO dto) {
		return sysRoleService.saveRole(dto);
	}


	@ApiOperation(value = "删除角色", notes = "删除角色")
	@PostMapping("/removeRole")
	public R<Boolean> removeRole(@Valid @RequestBody DeleteDto dto) {
		return sysRoleService.removeRole(dto);
	}


	@ApiOperation(value = "角色分配用户查询", notes = "角色分配用户查询")
	@GetMapping("/getRoleUserList")
	public R<List<RoleUserListVO>> getRoleUserList(@Valid RoleExistUserPageDTO dto) {
		return R.ok(sysRoleService.getRoleUserList(dto)) ;
	}
	@SysLog("给角色分配用户")
	@ApiOperation(value = "给角色分配用户", notes = "给角色分配用户")
	@PostMapping("/saveRoleUser")
	public R<Boolean> saveRoleUser(@Valid @RequestBody RoleUserSaveDTO dto) {
		return sysRoleService.saveRoleUser(dto);
	}

	/**
	 * 查询该角色已分配的菜单/按钮id
	 * @param
	 * @return
	 */
	@ApiOperation(value = "查询该角色已分配的菜单/按钮 树", notes = "查询该角色已分配的菜单/按钮 树")
	@GetMapping("/getRoleMenuTreeByRoleId")
	public R<List<RoleMenuTree>> getRoleMenuTreeByRoleId(@Valid RoleMenuTreeSelectDTO dto) {
		return R.ok(sysRoleService.getRoleMenuTreeByRoleId(dto)) ;
	}

	@SysLog("分配菜单/按钮")
	@ApiOperation(value = "分配菜单/按钮", notes = "分配菜单/按钮")
	@PostMapping("/saveRoleMenu")
	public R<Boolean> saveRoleMenu(@Valid @RequestBody RoleMenuDTO dto) {
		return sysRoleService.saveRoleMenu(dto);
	}

}
